package com.example.demo.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.example.demo.pojo.annotation.excel.ExcelColumn;
import com.example.demo.pojo.test.WsJGClassrecordReq;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;

import javax.xml.bind.JAXBException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 类描述信息
 *
 * @author TongDaShuai
 * @since 2022/3/31
 */
public class CustomExcelUtil {

    public static void main(String[] args) throws JAXBException {
        // 学时数据
        List<WsJGClassrecordReq> datas = new ArrayList<>();
        WsJGClassrecordReq wsJGClassrecordReq = new WsJGClassrecordReq();
        wsJGClassrecordReq.setRownum("1");
        wsJGClassrecordReq.setAccounTime("accoutTime");
        wsJGClassrecordReq.setAccountFlag("accountFlag");
        wsJGClassrecordReq.setBosId("bosid");
        wsJGClassrecordReq.setCarNum("carnum");
        wsJGClassrecordReq.setCoaCardCode("coachcode");
        wsJGClassrecordReq.setCoachFace("coachface");
        wsJGClassrecordReq.setCoachnum("coachnum");
        wsJGClassrecordReq.setCollectime("collectime");
        wsJGClassrecordReq.setCollector("collector");
        wsJGClassrecordReq.setCoachFaceOut("coachfaceout");
        wsJGClassrecordReq.setConfirmTime("confirmtime");
        wsJGClassrecordReq.setDuration("10");
        wsJGClassrecordReq.setDatabasetime("databasetime");
        wsJGClassrecordReq.setEndtime("endtime");
        wsJGClassrecordReq.setConnUser("connuser");
        wsJGClassrecordReq.setExamCode("examcode");
        wsJGClassrecordReq.setInscode("inscode");
        wsJGClassrecordReq.setIp("ip");
        wsJGClassrecordReq.setIsChecked("ischecked");
        wsJGClassrecordReq.setIsForced("isforced");
        wsJGClassrecordReq.setXmlStr(XmlParseUtil.beanToXml(wsJGClassrecordReq, "UTF-8", true, true));
        WsJGClassrecordReq wsJGClassrecordReq2 = new WsJGClassrecordReq();
        BeanUtil.copyProperties(wsJGClassrecordReq, wsJGClassrecordReq2);
        wsJGClassrecordReq2.setBosId("bosId2");
        wsJGClassrecordReq2.setRownum("2");
        wsJGClassrecordReq2.setXmlStr(XmlParseUtil.beanToXml(wsJGClassrecordReq2, "UTF-8", true, true));
        datas.add(wsJGClassrecordReq);
        datas.add(wsJGClassrecordReq2);

        // 设置字符串字段null默认为“0”(怡陆规定的)
        setNullDefault(wsJGClassrecordReq, "0");


        // 获取列别名
        Map<String, String> headerAlias = getHeaderAlias(WsJGClassrecordReq.class);
        System.out.println("列别名信息：" + headerAlias);

        // 导出的excel文件路径
        String exportPath = "C:/Users/Administrator/Desktop/怡陆学时数据.xlsx";
        // excel工具writer实例化
        ExcelWriter writer = ExcelUtil.getWriter(exportPath);
        writer.setHeaderAlias(headerAlias);

        // 设置默认的excel格式
        for (int i1 = 0; i1 < headerAlias.size(); i1++) {
            writer.setColumnWidth(i1, 25);
            // 标题头单元格格式
            CellStyle headCellStyle = writer.getHeadCellStyle();
            // 非标题头单元格格式
            CellStyle cellStyle = writer.getCellStyle();
            // 没有描边
            cellStyle.setBorderBottom(BorderStyle.NONE);
            cellStyle.setBorderTop(BorderStyle.NONE);
            cellStyle.setBorderLeft(BorderStyle.NONE);
            cellStyle.setBorderRight(BorderStyle.NONE);
            headCellStyle.setBorderBottom(BorderStyle.NONE);
            headCellStyle.setBorderTop(BorderStyle.NONE);
            headCellStyle.setBorderLeft(BorderStyle.NONE);
            headCellStyle.setBorderRight(BorderStyle.NONE);
        }
//        writer.writeHeadRow(titleJson);
        writer.write(datas, true);
        writer.close();
    }

    /**
     * 获取类的excel列别名map
     *
     * @param clazz 类对象
     * @param isRead 是否是读数据。true是读数据，false是写数据
     * @return
     */
    public static Map<String, String> getHeaderAlias(Class clazz, boolean isRead) {
        // 列别名map
        Map<String, String> headerAlias = new LinkedHashMap<>();
        // 通过反射获取clazz的列名和列的ExcelColumn注解信息
        Field[] fields = ReflectUtil.getFields(clazz);
        for (int i = 0; i < fields.length; i++) {
            ExcelColumn excelColumn = fields[i].getAnnotation(ExcelColumn.class);
            if (excelColumn == null) {
                continue;
            }
            if (isRead) {
                headerAlias.put(StrUtil.emptyToDefault(excelColumn.name(), fields[i].getName()), fields[i].getName());
            } else {
                headerAlias.put(fields[i].getName(), StrUtil.emptyToDefault(excelColumn.name(), fields[i].getName()));
            }
        }
        return headerAlias;
    }

    /**
     * 获取类的excel列别名map
     *
     * @param clazz 类对象
     * @return
     */
    public static Map<String, String> getHeaderAlias(Class clazz) {
        return getHeaderAlias(clazz, false);
    }

    /**
     * 给bean的null值设置默认值
     *
     * @param bean
     * @param defaultValue
     */
    public static void setNullDefault(Object bean, Object defaultValue) {
        for (Field field : ReflectUtil.getFields(bean.getClass())) {
            if (null == ReflectUtil.getFieldValue(bean, field)) {
                if (field.getType() == defaultValue.getClass()) {
                    ReflectUtil.setFieldValue(bean, field, defaultValue);
                }
            }
        }
    }

}
